/*
Copyright (c) 2012 the VATSIM Connector project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef __XPINN__MENUS_H__
#define __XPINN__MENUS_H__

#include "xpinn.h"
#include <functional>
#include <vector>
#include <XPLMMenus.h>

/**
  * A class for getting and setting the checked status of a menu item.
  */
class XPInnMenuItem
{
public:
  /**
   * Construct an uninitialized item which can be assigned to later.
   */
  XPInnMenuItem() : m_menu(0), m_index(0) {}

  /**
    * Return a Boolean indicating whether the item is checked.
    */
  bool isChecked();

  /**
    * Set whether or not the item is checked.
    */
  void setChecked(bool);

private:
  friend class XPInnMenuBuilder;
  XPInnMenuItem(XPLMMenuID menu, int index) : m_menu(menu), m_index(index) {}
  XPLMMenuID m_menu;
  int m_index;
};

/**
 * Class to build a submenu within the X-Plane plugins menu.
 *
 * The built menu will persist after the instance of this class is destroyed.
 */
class XPInnMenuBuilder
{
public:
  /**
   * The function type for "menu clicked" callbacks.
   */
  typedef std::tr1::function<void()> function_type;

  /**
   * Construct a builder for a menu with the given title.
   */
  XPInnMenuBuilder(char const* title);

  /**
   * Add an item to the menu.
   *
   * Returns a new item object which can be used to monitor the item's check
   * state. This can be ignored if the item will not have a check box.
   */
  XPInnMenuItem addItem(char const* label, function_type const& callback);

private:
  XPLMMenuID m_menu;
  static std::vector<function_type> s_callbacks;
  static void wrapperFunction(void* menuRefcon, void* itemRefcon);
};

#endif //__XPINN__MENUS_H__